package com.ruoyi.stock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.IsOrNo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.domain.StockMemberOrder;
import com.ruoyi.stock.domain.StockMemberRule;
import com.ruoyi.stock.domain.StockUser;
import com.ruoyi.stock.service.IStockMemberOrderService;
import com.ruoyi.stock.service.IStockMemberRuleService;
import com.ruoyi.stock.service.IStockUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/stock/stockmemberorder")
@Api( tags = "会员套餐订单交易")
public class StockMemberOrderController extends BaseController
{
    @Autowired
    private IStockMemberOrderService stockMemberOrderService;
    @Autowired
    private IStockMemberRuleService stockMemberRuleService;

    @Autowired
    private IStockUserService stockUserService;
    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "订单列表")
    public TableDataInfo list(StockMemberOrder stockMemberOrder)
    {
        startPage();
        List<StockMemberOrder> list = stockMemberOrderService.selectStockMemberOrderList(stockMemberOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockMemberOrder stockMemberOrder)
    {
        List<StockMemberOrder> list = stockMemberOrderService.selectStockMemberOrderList(stockMemberOrder);
        ExcelUtil<StockMemberOrder> util = new ExcelUtil<StockMemberOrder>(StockMemberOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stockMemberOrderService.selectStockMemberOrderById(id));
    }



    /**
     * 新增订单
     */
    @Log(title = "购买会员", businessType = BusinessType.INSERT)
    @PostMapping(value = "/buyOrderNotiry")
    @ApiOperation(value = "购买会员套餐回调")
    @Transactional
    public AjaxResult buyOrderNotiry(HttpServletRequest request, HttpServletResponse response)
    {
        System.out.println("+++++++进入了回调++++++++++++"+ response);
        stockMemberOrderService.payNotify(request,response);
        return  success(1);
    }



    /**
     * 新增订单
     */
    @Log(title = "购买会员", businessType = BusinessType.INSERT)
    @GetMapping(value = "/getFreeMember")
    @ApiOperation(value = "获取免费会员套餐")
    @Transactional
    public AjaxResult getFreeMember()
    {
        StockUser stockUser=new StockUser();
        stockUser.setSysUserid(SecurityUtils.getUserId());
        stockUser= stockUserService.selectStockUserList(stockUser).get(0);
        if(stockUser.getIsMember()!=null){
            if(stockUser.getIsMember()==1){
                throw new GlobalException("已经是会员，不能在领取");
            }
        }
        StockMemberOrder stockMemberOrder=new StockMemberOrder();
        stockMemberOrder.setMemberCardId(1l);
        stockMemberOrderService.buyOrderUserHandel(stockMemberOrder);
        return  success(1);
    }



    /**
     * 新增订单
     */
    @Log(title = "购买会员", businessType = BusinessType.INSERT)
    @PostMapping(value = "/buyOrder")
    @ApiOperation(value = "购买会员套餐")
    @Transactional
    public AjaxResult buyOrder(@RequestBody  StockMemberOrder stockMemberOrder)
    {
        StockMemberRule stockMemberRule= stockMemberRuleService.selectStockMemberRuleById(stockMemberOrder.getMemberCardId());
         ///////////////////////
        stockMemberOrder.setMemberDays(stockMemberRule.getValidDays()+"");
        stockMemberOrder.setMemberCardName(stockMemberRule.getName());
        stockMemberOrder.setOrderNum(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS,new Date())+(System.currentTimeMillis()% 10000+""));
        stockMemberOrder.setPayMonery(stockMemberRule.getPrice()+"");
       // stockMemberOrder.setPayDate(new Date());
        stockMemberOrder.setOrderState(1l);
        stockMemberOrder.setCreateTime(new Date());
        stockMemberOrder.setCreateBy(SecurityUtils.getUsername());
        stockMemberOrder.setUserId(SecurityUtils.getUserId());
        return  success(stockMemberOrderService.wxStockOrderPay(stockMemberOrder));
    }


    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockMemberOrder stockMemberOrder)
    {
        StockMemberRule stockMemberRule= stockMemberRuleService.selectStockMemberRuleById(stockMemberOrder.getMemberCardId());
        StockUser stockUser=new StockUser();
        long sysuserid= SecurityUtils.getUserId();
        stockUser.setSysUserid(sysuserid);
        stockUser= stockUserService.selectStockUserList(stockUser).get(0);
        stockUser.setIsMember(IsOrNo.是.getCode());
        if(stockMemberOrder.getMemberCardId()==1l){
            stockUser.setIsFreemember(IsOrNo.是.getCode());
        }
        stockUser.setMemberDays(stockMemberRule.getValidDays()+"");
        stockUser.setMemberType(stockMemberRule.getName());
        stockUserService.updateStockUser(stockUser);
        stockMemberOrder.setOrderNum(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS,new Date())+(System.currentTimeMillis()% 10000+""));
        stockMemberOrder.setPayMonery(stockMemberRule.getPrice()+"");
        stockMemberOrder.setCreateTime(new Date());
        stockMemberOrder.setCreateBy(SecurityUtils.getUsername());
        stockMemberOrder.setUserId(sysuserid);
        return toAjax(stockMemberOrderService.insertStockMemberOrder(stockMemberOrder));
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockMemberOrder stockMemberOrder)
    {
        return toAjax(stockMemberOrderService.updateStockMemberOrder(stockMemberOrder));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockMemberOrderService.deleteStockMemberOrderByIds(ids));
    }
}
